#include <iostream>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

std::string Bpwd(char* pwd);

void Puser()
{
    const char* x = getenv("USER");
    const char* y = getenv("HOSTNAME");
    char* z = getenv("PWD");
    std::string h = Bpwd(z);
    //[wxs@hcss-ecs-77f3 text-12-5]
    printf("[%s@%s %s]# ",x,y,h.c_str());
}

std::string Bpwd(char* pwd)
{
    std::string dir = pwd;
    if(dir == "/") return dir;
    auto pos = dir.rfind("/");
    if(pos == std::string::npos) return "BUG?";
    return dir.substr(pos+1);
}

bool GetCommandLine(char* out,int size)
{
    char* c = fgets(out,size,stdin);
    out[strlen(out)-1] = 0;
    if(c)
        return true;
    else
        return false;
}

//
void Command()
{
    //Puser();
    char* agrv[128];
    char Cmand[1024];
    int i = 0;
    GetCommandLine(Cmand,1024);
    agrv[i++] = strtok(Cmand, " ");
    while((bool)(agrv[i++] = strtok(NULL, " ")));
    int n = fork();
    if (n == 0)
    execvp(agrv[0],agrv);
    wait(NULL);
}

int main()
{
    while(1){
        Puser();
        Command();
    }
    return 0;
}

